package 实验六;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;



public class Student {
	public String No,Name,Sex;
	public Course[] SC = new Course[100];
	public Student() {
	}
	public Student(String no,String na,String sx){
		No = no;
		Name = na;
		Sex = sx;
	}
	public void print() {
		System.out.println(this.No+" "+this.Name+" "+this.Sex);
	}
	public void addSCourse(Course c) throws IOException {
		for (int i=0;i<=SC.length-1;i++) {
			if (SC[i] != null && i == SC.length-1)
				break;
			if (SC[i] == null) {
				SC[i] = c;
				break;
 			}
		}
		try {
			FileOutputStream filepath = new FileOutputStream("D:\\SC.txt");
			ObjectOutputStream output = new ObjectOutputStream(filepath);
			output.writeObject(SC);
			output.close();
		}catch(FileNotFoundException f) {
			System.out.println("An error has occured.");
		}
	}
	public void deleteCourse(Course c) throws IOException {
		try {
			FileInputStream filepath = new FileInputStream("D:\\SC.txt");
			ObjectInputStream input = new ObjectInputStream(filepath);
			SC = (Course[])input.readObject();
			input.close();
		}catch(FileNotFoundException f) {
			System.out.println("An error has occured.");
		}catch(ClassNotFoundException cn) {
			System.out.println("An error has occured.");
		}
		for(int j=0;j<=SC.length-1;j++) {
			if(SC[j] != null) {
				if(SC[j].Cname == c.Cname) {
					SC[j] = null;
					break;
				}
			}
		}
		
	}

	public void printSC() {
		System.out.println("********ѧ"+this.Name+"Ŀα********\n");
		for(int i=0;i<=this.SC.length-1;i++) {
			if(this.SC[i] != null) {
				this.SC[i].printCourse();
			}
		}
	}
}